package com.lemon.smartsafety.ui.adapter.safe.data;

import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ldzs.recyclerlibrary.view.SpacesItemDecoration;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.common.EvidenceDataI;
import com.lemon.smartsafety.entity.event.datacoll.BuildingCheckItemDeleteEvent;
import com.lemon.smartsafety.entity.event.datacoll.BuildingCheckItemEditEvent;
import com.lemon.smartsafety.entity.event.datacoll.BuildingCheckItemViewFlowEvent;
import com.lemon.smartsafety.entity.safe.company.BuildingCheckItemViewNfcEvent;
import com.lemon.smartsafety.entity.safe.company.BuildingCheckItemViewQrcodeEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingCheckItemVo;
import com.lemon.smartsafety.ui.adapter.common.HorizontalEvidenceAdapter;
import com.lemon.smartsafety.utils.common.DrawableUtils;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.event.patrol.OneKeyBindEvent;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class BuildingCheckItemAdapter extends LemonRcvAdapter<CompanyBuildingCheckItemVo> {
    public BuildingCheckItemAdapter(@Nullable List<CompanyBuildingCheckItemVo> data) {
        super(data);
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new BuildingCheckItemItem();
    }

    public class BuildingCheckItemItem implements AdapterItem<CompanyBuildingCheckItemVo> {
        LinearLayout rippleEdit;
        LinearLayout   rippleDelete;
        RelativeLayout rippleViewFlow;
        CardView     mainCardView;
        TextView     tvTitle;
        ImageView    ivEdit;
        ImageView    ivDelete;
        ImageView    ivNfc;
        ImageView    ivQrcode;

        TextView     tvPatrol;
        TextView     tvCheckWeibao;
        TextView     tvCycle;
        TextView     tvFirePart;
        TextView     tvRemark;

        TextView     tvViewFlow;
        TextView     tvViewNfc;
        TextView     tvViewQrcode;
        TextView     tvViewOneKey;

        TextView     tvViewCardId;

        RecyclerView recyclerView;

        SpacesItemDecoration mItemDecoration;

        private CompanyBuildingCheckItemVo mData;
        private int                        mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_build_check_item;
        }

        @Override
        public void bindViews(View root) {

            rippleDelete = (LinearLayout) root.findViewById(R.id.rippleDelete);
            rippleEdit = (LinearLayout) root.findViewById(R.id.rippleEdit);
            rippleViewFlow = (RelativeLayout) root.findViewById(R.id.rippleViewFlow);

            mainCardView = (CardView) root.findViewById(R.id.main_card_view);
            tvTitle = (TextView) root.findViewById(R.id.tv_title);
            ivDelete = (ImageView) root.findViewById(R.id.iv_delete);

            ivNfc = (ImageView) root.findViewById(R.id.iv_nfc);
            ivQrcode = (ImageView) root.findViewById(R.id.iv_qrcode);

            Drawable src = ivDelete.getDrawable();
            ivDelete.setImageDrawable(DrawableUtils.$tint(src, ivDelete.getContext().getResources().getColor(R.color.red)));


            ivEdit = (ImageView) root.findViewById(R.id.iv_edit);
            ivEdit.setImageDrawable(DrawableUtils.$tint(ivEdit.getDrawable(), ivEdit.getContext().getResources().getColor(R.color.colorPrimary)));


            tvPatrol = (TextView) root.findViewById(R.id.tv_patrol);
            tvCheckWeibao = (TextView) root.findViewById(R.id.tv_check_weibao);
            tvCycle = (TextView) root.findViewById(R.id.tv_cycle);
            tvFirePart = (TextView) root.findViewById(R.id.tv_fire_part);
            tvRemark = (TextView) root.findViewById(R.id.tv_remark);


            tvViewFlow = (TextView) root.findViewById(R.id.tv_view_flow);
            tvViewFlow.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

            tvViewNfc = (TextView) root.findViewById(R.id.tv_view_Nfc);
            tvViewNfc.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

            tvViewQrcode = (TextView) root.findViewById(R.id.tv_view_Qrcode);
            tvViewQrcode.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

            tvViewOneKey = (TextView) root.findViewById(R.id.tv_view_one_key);
            tvViewOneKey.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
            tvViewOneKey.setTextColor(0xFFFF0000);

            recyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);

            int spacingInPixels = recyclerView.getContext().getResources().getDimensionPixelSize(R.dimen.item_grid_divider_line);
            mItemDecoration = new SpacesItemDecoration(spacingInPixels);
            recyclerView.addItemDecoration(mItemDecoration);

            tvViewCardId = (TextView) root.findViewById(R.id.card_id_tv);  // 标签的ID号

        }

        @Override
        public void setViews() {//这里的事件基本都在 BuildingCheckItemListFragment 里面处理
            rippleDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingCheckItemDeleteEvent event = new BuildingCheckItemDeleteEvent();
                    event.setData(mData);
                    event.setTag(BuildingCheckItemDeleteEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });
            rippleEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingCheckItemEditEvent event = new BuildingCheckItemEditEvent();
                    event.setData(mData);
                    event.setTag(BuildingCheckItemEditEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });
            mainCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingCheckItemEditEvent event = new BuildingCheckItemEditEvent();
                    event.setData(mData);
                    event.setTag(BuildingCheckItemEditEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });


            //查看流程
            tvViewFlow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingCheckItemViewFlowEvent event = new BuildingCheckItemViewFlowEvent();
                    event.setData(mData);
                    event.setTag(BuildingCheckItemViewFlowEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });


            //绑定NFC
            tvViewNfc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingCheckItemViewNfcEvent event = new BuildingCheckItemViewNfcEvent();
                    event.setData(mData);
                    event.setTag(BuildingCheckItemViewNfcEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });


            //绑定二维码
            tvViewQrcode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingCheckItemViewQrcodeEvent event = new BuildingCheckItemViewQrcodeEvent();
                    event.setData(mData);
                    event.setTag(BuildingCheckItemViewQrcodeEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });

            //一键绑定 二维码和NFC
            tvViewOneKey.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    OneKeyBindEvent event = new OneKeyBindEvent();
                    event.setData(mData);
                    event.setTag(OneKeyBindEvent.class.getName());
                    AppService.getBus().post(event);
                }
            });

        }

        @Override
        public void handleData(CompanyBuildingCheckItemVo data, int position) {
            mData = data;
            mPosition = position;

            tvTitle.setText(StringUtils.nullValue(data.getObjectTitle()));

            //changed by Andrew.yin 2017/9/19 begin. 修改为最新的判断条件；


//            if(data.getBindingNfcStatus().equals("1")){
//                ivNfc.setVisibility(View.VISIBLE);
//                //tvViewNfc.setVisibility(View.GONE);
//            }else{
//                ivNfc.setVisibility(View.GONE);
//                //tvViewNfc.setVisibility(View.VISIBLE);
//            }
//
//            if(data.getBindingQrcodeStatus().equals("1")){
//                ivQrcode.setVisibility(View.VISIBLE);
//                //tvViewQrcode.setVisibility(View.GONE);
//            }else{
//                ivQrcode.setVisibility(View.GONE);
//                //tvViewQrcode.setVisibility(View.VISIBLE);
//            }

            // 当为0时，为未绑定；
            // 有NFC或QR值时，为已绑定
            if(data.getBindingNfcStatus().equals("0")){
                ivNfc.setVisibility(View.GONE);
            }else{
                ivNfc.setVisibility(View.VISIBLE);
            }

            if(data.getBindingQrcodeStatus().equals("0")){
                ivQrcode.setVisibility(View.GONE);
            }else{
                ivQrcode.setVisibility(View.VISIBLE);
            }
            //changed by Andrew.yin 2017/9/19 end.

            tvViewCardId.setText("标签ID: " + CommonUtil.avoidNullString(data.getLabelCode()));


            if (StringUtils.getBoolean(data.getIsPatrol())) {
                tvPatrol.setText(DataInfo.getStatusString("是", DataInfo.ColorStatus.STATUS_BLUE));
            } else {
                tvPatrol.setText(DataInfo.getStatusString("否", DataInfo.ColorStatus.STATUS_GRAY));
            }

           //获得的是巡更点的数据，改为维保数据
            if (StringUtils.getBoolean(data.getIsKeepCheck())) {
                tvCheckWeibao.setText(DataInfo.getStatusString("是", DataInfo.ColorStatus.STATUS_GREEN));
            } else {
                tvCheckWeibao.setText(DataInfo.getStatusString("否", DataInfo.ColorStatus.STATUS_GRAY));
            }
            tvCycle.setText(data.getCheckValidity() + data.getUnitName());

            if (StringUtils.nullValue(data.getRemark()).length() == 0) {
                tvRemark.setVisibility(View.GONE);
            } else {
                tvRemark.setVisibility(View.VISIBLE);
            }

            tvFirePart.setText(StringUtils.nullValue(data.getPlaceTypeName()));

            tvRemark.setText(data.getRemark());


            List<EvidenceDataI> result = new ArrayList<>();
            result.addAll(data.getPlayViewList());
            if (result.size() != 0) {
                /**
                 * 证据视图部分
                 */
                HorizontalEvidenceAdapter evidenceAdapter = new HorizontalEvidenceAdapter(result);
                LinearLayoutManager       layoutManager   = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.HORIZONTAL, false);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(evidenceAdapter);
            }else{
                HorizontalEvidenceAdapter evidenceAdapter = new HorizontalEvidenceAdapter(result);
                LinearLayoutManager       layoutManager   = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.HORIZONTAL, false);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(evidenceAdapter);
            }

        }
    }
}
